﻿// ******************************************************************
// * Copyright(c)  Steven Lu
// * Description   : Entity_PROD_COOPERATION_ORDER
// * CreateDate    : 2012/4/26 16:11:47
// * Creater       : CodeSmith
// * LastChangeDate: 2012/4/26 16:11:47
// * LastChanger   : 2012/4/26 16:11:47
// * Version Info  : 1.0 For ESOL
// ******************************************************************

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Suzsoft.Smart.Data;
using ESOL.Common.Entity;
using ESOL.Business.Mst;
using ESOL.Common;

namespace ESOL.Business.Prod
{
    /// <summary>
    /// ProdCooperationOrder
    /// </summary>
    public partial class ProdCooperationOrderBs
    {
        #region GetByID
        /// <summary>
        /// Get Entity
        /// </summary>
        /// <param name="objProdCooperationOrderEntity">Entity</param>
        /// <returns>Entity</returns>
        public static ProdCooperationOrderEntity GetProdCooperationOrder(ProdCooperationOrderEntity objProdCooperationOrderEntity)
        {
            return DataAccess.SelectSingle<ProdCooperationOrderEntity>(objProdCooperationOrderEntity);
        }
        #endregion


        #region Insert
        /// <summary>
        /// Insert Data
        /// </summary>
        /// <param name="objProdCooperationOrderEntity">Entity</param>
        /// <returns>true Insert Succeed; false Insert Fail</returns>
        public static bool InsertProdCooperationOrder(ProdCooperationOrderEntity objProdCooperationOrderEntity)
        {
            return DataAccess.Insert(objProdCooperationOrderEntity);
        }

        public static bool InsertProdCooperationOrder(List<ProdCooperationOrderEntity> objProdCooperationOrderEntityList)
        {
            return DataAccess.Insert(objProdCooperationOrderEntityList);
        }
        #endregion


        #region Update
        /// <summary>
        /// Update Data
        /// </summary>
        /// <param name="objProdCooperationOrderEntity">Entity</param>
        /// <returns>true Update Succeed; false Update Fail</returns>
        public static bool UpdateProdCooperationOrder(ProdCooperationOrderEntity objProdCooperationOrderEntity)
        {
            return DataAccess.Update(objProdCooperationOrderEntity);
        }

        public static bool UpdateProdCooperationOrder(List<ProdCooperationOrderEntity> objProdCooperationOrderEntityList)
        {
            return DataAccess.Update<ProdCooperationOrderEntity>(objProdCooperationOrderEntityList);
        }
        #endregion


        #region Delete
        /// <summary>
        /// Delete Data
        /// </summary>
        /// <param name="objProdCooperationOrderEntity">Entity</param>
        /// <returns>true Delete Succeed; false Delete Fail</returns>
        public static bool DeleteProdCooperationOrder(ProdCooperationOrderEntity objProdCooperationOrderEntity)
        {
            return DataAccess.Delete(objProdCooperationOrderEntity);
        }

        public static bool DeleteProdCooperationOrder(List<ProdCooperationOrderEntity> objProdCooperationOrderEntityList)
        {
            return DataAccess.Delete<ProdCooperationOrderEntity>(objProdCooperationOrderEntityList);
        }
        #endregion


        #region GetAll
        /// <summary>
        /// Get All Data
        /// </summary>
        /// <returns>Back All Data<returns>
        public static DataSet GetProdCooperationOrderAll()
        {
            return DataAccess.SelectAllDataSet<ProdCooperationOrderEntity>();
        }
        /// <summary>
        /// Get All Data List
        /// </summary>
        /// <returns>Back All Data List<returns>
        public static List<ProdCooperationOrderEntity> GetProdCooperationOrderAllList()
        {
            return DataAccess.SelectAll<ProdCooperationOrderEntity>();
        }
        #endregion

        #region Functions
        public static bool SaveCooperationOrder(bool IsNew,ProdCooperationOrderEntity entity)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    if (IsNew)
                    {
                        string CooperationOrderCode = "";
                        if (MstCodeRuleBs.GetCurrentOrderCode("CooperationOrder", out CooperationOrderCode, broker) == false)
                        {
                            broker.RollBack();
                            return false;
                        }
                        entity.OrderCode = CooperationOrderCode;
                        DataAccess.Insert(entity, broker);
                    }
                    DataAccess.Update(entity, broker);
                    ProdProductTechnicEntity mProductTechnic = ProdProductTechnicBs.GetProdProductTechnicByID(entity.ProductTechnicId);
                    ProdPlanOrderItemEntity mPlanOrderItem = ProdPlanOrderItemBs.GetProdPlanOrderItemByID(mProductTechnic.PlanOrderItemId);
                    mPlanOrderItem.Status = "POI06";
                    DataAccess.Update(mPlanOrderItem, broker);
                    broker.Commit();
                    return true;
                }
                catch(Exception ex)
                {
                    broker.RollBack();
                    return false;
                }
            }
        }

        public static DataTable GetDtAllByCondition(string sCondition, string sRowCount, string sOrderBy)
        {
            string sql = CommonFunctions.GetSelect(sRowCount) + @" pco.*,ppo.PLAN_ORDER_CODE,sppai.PRODUCT_SPEC_OUT as CONTRACT_MODEL,ppoi.ORDER_ITEM_CODE as WORKING_ORDER_CODE,
mp.PRODUCT_NAME,mp.PRODUCT_CODE,mp.DESCRIPTION,mci.ITEM_NAME as UNIT,pt.TECHNIC_NAME,pt.TECHNIC_CODE,ms.SUPPLIER_NAME 
from PROD_COOPERATION_ORDER pco
left join PROD_PRODUCT_TECHNIC ppt on pco.PRODUCT_TECHNIC_ID=ppt.PRODUCT_TECHNIC_ID
left join PROD_PLAN_ORDER_ITEM ppoi on ppt.PLAN_ORDER_ITEM_ID=ppoi.PLAN_ORDER_ITEM_ID
left join PROD_PLAN_ORDER ppo on ppoi.PLAN_ORDER_ID=ppo.PLAN_ORDER_ID
left join SALES_PROD_PLAN_APPLICATION_ITEM sppai on ppo.APPLICATION_ITEM_ID=sppai.APPLICATION_ITEM_ID
left join MST_PRODUCT mp on ppoi.PRODUCT_ID=mp.PRODUCT_ID
left join PROD_TECHNIC pt on ppt.TECHNIC_ID=pt.TECHNIC_ID
left join MST_COMMON_ITEM mci on mp.UNIT=mci.ITEM_CODE
left join MST_SUPPLIER ms on pco.SUPPLIER_ID=ms.SUPPLIER_ID
where 1=1 ";
            if (string.IsNullOrEmpty(sCondition) == false)
            {
                sql += sCondition;
            }
            if (string.IsNullOrEmpty(sOrderBy) == false)
            {
                sql += sOrderBy;
            }
            return DataAccess.SelectDataSet(sql).Tables[0];
        }

        //临时使用，以后要改
        public static DataTable GetAllSupplier()
        {
            string sql = @"select distinct SUPPLIER_NAME from PROD_COOPERATION_ORDER";
            return DataAccess.SelectDataSet(sql).Tables[0];
        }

        public static ProdCooperationOrderEntity GetCooperationOrderByProductTechnicID(string sProductTechnicID)
        {
            string sql = "select * from PROD_COOPERATION_ORDER where PRODUCT_TECHNIC_ID='" + sProductTechnicID+"'";
            List < ProdCooperationOrderEntity> list=DataAccess.Select<ProdCooperationOrderEntity>(sql);
            return list[0];
        }
        #endregion
    }


}

